<!--
 * @Author: HeAo
 * @Date: 2021-10-11 14:49:18
 * @LastEditTime: 2021-10-11 16:05:50
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day03\2-组件通信-父传子.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue组件传参-父传子</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="app">
    <!-- 3.组件使用 -->
    <button @click="attr2.username = 'tom'">修改父组件数据</button>
    <my-com :num="1" :parent-data="attr2"></my-com>
    <!-- <my-com :num="1" attr1="he"></my-com> -->
  </div>
  <script>
    // 1.组件定义
    let component1 = {
      // props: ['num', 'attr1', 'parentData'],
      props: {
        num: [Number, String],
        attr1: {
          type: String,
          // required: true,
          default: '默认值',
          validator(value) {
            return value.length >= 3
          }
        },
        parentData: {
          type: Object,
          // default: {
          //   username: '李四',
          //   age: 20
          // }
          default() {
            return {
              username: '李四',
              age: 20
            }
          }
        }
      },
      data() {
        return {
          msg: '子组件一'
        }
      },
      template: `
        <div>
          <h3>{{msg}}</h3> 
          <h2>父组件传递过来的数据：{{attr1}}-{{parentData}}</h2> 
          <h4>{{num}}-{{typeof num}}</h4>
        </div>
      `
    }

    let vm = new Vue({
      el: '#app',
      data: {
        attr1: '我是父组件数据',
        attr2: {
          username: 'zhangsan',
          age: 12
        }
      },
      methods: {},
      // 2.局部注册组件
      components: {
        'my-com': component1
      }
    })
  </script>
</body>

</html>